﻿using CRM.Infrastructure.Dto.Request.Customer;
using CRM.Infrastructure.Dto.Response;
using CRM.Infrastructure.Dto.Response.Customer;
using CRM.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CRM.Api.Admin.Controllers
{
    [Authorize(Roles = "admin")]
    public class CustomerController : BaseController
    {
        private readonly I_Customer_Service _customer_Service;
        public CustomerController(I_Customer_Service customer_Service, I_Login_Service login_Service) : base(login_Service)
        {
            _customer_Service = customer_Service;


        }

        /// <summary>
        /// 获取客户列表 只能查看自己所负责的客户和公海客户
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Customers(int? page = 1, int? limit = 10, string? key = "")
        {
            var user = await this.Get_Curr_User();

            var result = await _customer_Service.Get_Customers(page ?? 1, limit ?? 10, key ?? "", user.UId);
            return Ok(result);
        }
        /// <summary>
        /// 获取公海客户
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Customers_HighSeas(int? page = 1, int? limit = 10, string? key = "")
        {


            var result = await _customer_Service.Get_Customers_HighSeas(page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }
        

        /// <summary>
        /// 新增客户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_Customer(Customer_Request_Dto dto)
        {
            var user = await this.Get_Curr_User();
            dto.author = user.UId;
            var result = await _customer_Service.Add_Customer(dto);
            return Ok(result);
        }

        /// <summary>
        /// 新增跟进记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_Record(Record_Request_Dto dto)
        {
            var user = await this.Get_Curr_User();
            dto.author = user.UId;
            var result = await _customer_Service.Add_Record(dto);
            return Ok(result);
        }


        /// <summary>
        /// 获取单个客户的跟进记录
        /// </summary>
        /// <param name="id">客户ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Single_Customer_Records(string id)
        {
            Api_Response_Dto result = await _customer_Service.Get_Single_Customer_Records(id);
            return Ok(result);
        }

        /// <summary>
        /// 新增VIP 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<IActionResult> Add_Vip(Vip_Request_Dto dto)
        {
            var user = await this.Get_Curr_User();
            dto.responsible = user.UId;
            var result = await _customer_Service.Add_Vip(dto);
            return Ok(result);
        }
        /// <summary>
        /// 等级下拉框
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<IActionResult> Get_Level()
        {
            var result = await _customer_Service.Get_Level();
            return Ok(result);
        }
        /// <summary>
        /// 来源下拉框
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<IActionResult> Get_Origin()
        {
            var result = await _customer_Service.Get_Origin();
            return Ok(result);
        }
        /// <summary>
        /// 类型下拉框
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<IActionResult> Get_Type()
        {
            var result = await _customer_Service.Get_Type();
            return Ok(result);
        }
        /// <summary>
        /// 负责人下拉框
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<IActionResult> Get_user()
        {
            var result = await _customer_Service.Get_user();
            return Ok(result);
        }
        /// <summary>
        /// 获取客户详情
        /// </summary>
        /// <param name="id">客户ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Customer_Details(string id)
        {
            Api_Response_Dto result = await _customer_Service.Get_Customer_Details(id);
            return Ok(result);
        }
        /// <summary>
        /// 修改客户详情
        /// </summary>
        /// <param name="id">客户ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Customer_Details(Customer_Response_Dto dto)
        {
            Api_Response_Dto result = await _customer_Service.Update_Customer_Details(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取公海客户
        /// </summary>
        /// <param name="id">客户ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update_Customer(string id)
        {
            var user = await this.Get_Curr_User();
            Api_Response_Dto result = await _customer_Service.Update_Customer(id,user.UId);
            return Ok(result);
        }

        
    }
}
